import itertools  # 用于操作迭代对象的函数
# --------------------几个“无限”迭代器------------------
natural = itertools.count(1)
for num in natural:
    if num <= 5:
        print(num)
    else:
        break
# --------通过takewhile()等函数根据条件判断来截取出一个有限的序列---
ns = itertools.takewhile(lambda x: x < 10, natural)
print(list(ns))
cs = itertools.cycle("ABC")  # 无限循环cycle内的数据
# for char in cs:
#     print(char)
ic = itertools.chain("ABC", "JIA")  # 可以串联迭代对象
for ico in ic:
    print(ico)
re = itertools.repeat("o", 10)  # 可以无限重复object，也可限定重复次数
for r in re:
    print(r)
# ---------------------------groupby()把迭代器中相邻的重复元素挑出来放在一起------
gb = itertools.groupby("AAAAACCCCCDDDWWWWeeeEEEEEEANNNNNN")
for key, values in gb:
    print(key)
    print(list(values))
for key, group in itertools.groupby('AaaBBbcCAAa', lambda c: c.upper()):
    print(key, list(group))
